package com.rs.adminweb.commons.web;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jaxplat.JaxplatBean;
import com.jaxplat.delegate.CommonsJaxplatDelegate;
import com.jaxplat.exceptions.GeneralException;
import com.jaxplat.util.StringUtils;

public abstract class FileUploadUtils
{
    protected static final Log logger = LogFactory.getLog(FileUploadUtils.class);

    public static String getUplaodRootPath(HttpServletRequest request)
    {
        return request.getRealPath("/upload/");
    }

    public static File parseUploadFilePath(String uploadRootPath, FileUploadEntity entity)
    {
        String path = uploadRootPath;
        String relativePath = entity.getPath();

        File fpath = new File(path);

        if ((!StringUtils.isEmpty(relativePath)) && (!"/".equals(relativePath)))
        {
            fpath = new File(fpath, relativePath);
        }
        else
        {
            entity.setPath("/");
        }

        return fpath;
    }

    public static List findUploadedFiles(String busiTable, String busiId) throws GeneralException
    {
        CommonsJaxplatDelegate delegate = (CommonsJaxplatDelegate) JaxplatBean.getBean("commonsJaxPlatDelegate");
        String sql = "select * from plat_file_upload where busi_id=? and busi_table=? order by sequence,upload_id";
        List list = delegate.findEntities(sql, new String[] { busiId, busiTable }, FileUploadEntity.class);
        return list;
    }

    public static FileUploadEntity getFileUploadEntity(String uploadId) throws GeneralException
    {
        FileUploadEntity entity = new FileUploadEntity();
        entity.setUploadId(new Long(uploadId));
        CommonsJaxplatDelegate delegate = (CommonsJaxplatDelegate) JaxplatBean.getBean("commonsJaxPlatDelegate");

        return (FileUploadEntity) delegate.findEntity(entity);
    }

    public static String getFileDownloadLinks(HttpServletRequest request, String busiTable, String busiId) throws GeneralException
    {
        String context = request.getContextPath();
        String prefix = "<a href='" + context + "/commons/swfupload/upload.do?method=download&uploadId=";
        String middle = "'>";
        String suffix = "</a> \n";

        StringBuffer links = new StringBuffer();
        List files = findUploadedFiles(busiTable, busiId);
        for (Iterator iter = files.iterator(); iter.hasNext();)
        {
            FileUploadEntity file = (FileUploadEntity) iter.next();
            links.append(prefix).append(file.getUploadId()).append(middle).append(file.getName()).append(suffix);
        }

        return links.toString();
    }

    public static String getFileDownloadLinksForDesc(HttpServletRequest request, String busiTable, String busiId) throws GeneralException
    {
        String context = request.getContextPath();
        String prefix = "<a href='" + context + "/commons/swfupload/upload.do?method=download&uploadId=";
        String middle = "'>";
        String suffix = "</a> \n";

        StringBuffer links = new StringBuffer();
        List files = findUploadedFiles(busiTable, busiId);
        for (Iterator iter = files.iterator(); iter.hasNext();)
        {
            FileUploadEntity file = (FileUploadEntity) iter.next();
            links.append(prefix).append(file.getUploadId()).append(middle).append(file.getDescription()).append(suffix);
        }

        return links.toString();
    }

    public static String getNiceFileSize(int size)
    {
        int _K = 1024;
        int _M = 1048576;
        if (size < _M)
        {
            if (size < _K)
            {
                return size + "B";
            }
            return Math.ceil(size / _K) + "K";
        }

        return Math.ceil(100 * size / _M) / 100.0D + "M";
    }
}